//
//  SearchView.swift
//  个人微博
//
//  Created by guo3920591 on 16/4/16.
//  Copyright © 2016年 guo3920591. All rights reserved.
//

import UIKit

class SearchView: UIView {
    

    

 /// 搜索框
    @IBOutlet weak var searchtEXTfield: UITextField!
 /// 搜索框右边的约束
    @IBOutlet weak var rightCons: NSLayoutConstraint!
    
    @IBOutlet weak var cancelBtn: UIButton!
    
    override func awakeFromNib() {
        
        searchtEXTfield.leftView = spoligtView
        
        
        spoligtView.frame.size = CGSizeMake(searchtEXTfield.frame.height, searchtEXTfield.frame.height)
        
        //设置编辑模式
        searchtEXTfield.leftViewMode = .Always
        
        
        }
    
    @IBAction func didiClickBtn() {
        
        //改变约束
        rightCons.constant = 0
        
        UIView.animateWithDuration(0.5) { () -> Void in
            //立刻马上布局
            self.layoutIfNeeded()
        }
        
        searchtEXTfield.resignFirstResponder()
        
        
        
    }
    /// 开始编辑搜索框
    @IBAction func didClickEdit(sender: UITextField) {
        
        //改变约束
        rightCons.constant = cancelBtn.frame.width
        
        UIView.animateWithDuration(0.5) { () -> Void in
            //立刻马上布局
            self.layoutIfNeeded()
        }
    
    
    }
    
// MARK: - 私有属性
    
    private lazy var spoligtView: UIImageView = {
        
        let spotView = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        
        spotView.contentMode = .Center
    
        
        return spotView
        
        
    }()
}

extension SearchView {
    
    class  func searchView()-> SearchView {
        
        
        return (NSBundle.mainBundle().loadNibNamed("SearchView", owner: nil, options: nil).last as? SearchView)!
    
    }
    
    
}
